package com.fangtao.filter;

import javax.servlet.*;
import java.io.IOException;

/**
 * @ClassName CharacterEncodingFilter
 * @Description TODO
 * @date 2022/9/20 11:12
 * @author: fangtao
 * @Version 1.0
 */
public class CharacterEncodingFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("过滤器初始化了");
    }

    @Override
    public void doFilter(ServletRequest Request, ServletResponse Response, FilterChain filterChain) throws IOException, ServletException {
        Request.setCharacterEncoding("utf-8");
        Response.setCharacterEncoding("utf-8");
        Response.setContentType("text/html;charset=UTF-8");
//        System.out.println("过滤器执行之前");
        filterChain.doFilter(Request,Response);//如果不写这个程序会在这个地方被拦截
//        System.out.println("过滤器执行之后");
    }


    @Override
    public void destroy() {
        Filter.super.destroy();
    }
}
